package com.vilen.cms.controller;

import com.vilen.basic.util.EnumUtil;
import com.vilen.cms.auth.AuthClass;
import com.vilen.cms.dto.AjaxObj;
import com.vilen.cms.dto.TreeDto;
import com.vilen.cms.model.Channel;
import com.vilen.cms.model.ChannelTree;
import com.vilen.cms.model.ChannelType;
import com.vilen.cms.service.IChannelService;
import org.jboss.logging.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by vilen on 2017/2/12.
 */
@Controller
@RequestMapping("/admin/channel")
@AuthClass
public class ChannelController {

    private IChannelService channelService;

    public IChannelService getChannelService() {
        return channelService;
    }

    @Inject
    public void setChannelService(IChannelService channelService) {
        this.channelService = channelService;
    }

    @RequestMapping("/channels")
    public String list(Model model) {
        //model.addAttribute("treeDatas", JsonUtil.getInstance().obj2json(channelService.generateTree()));
        return "channel/list";
    }

    @RequestMapping("/channels/{pid}")
    public String listChild(@PathVariable Integer pid, @Param Integer refresh, Model model) {
        Channel pc = null;
        if (refresh == null) {
            model.addAttribute("refresh", 0);
        } else {
            model.addAttribute("refresh", 1);
        }
        if (pid == null || pid == 0) {
            pc = new Channel();
            pc.setName(Channel.ROOT_NAME);
            pc.setId(Channel.ROOT_ID);
        } else {
            pc = channelService.load(pid);
        }
        model.addAttribute("pc", pc);
        model.addAttribute("channels", channelService.listByParent(pid));
        return "channel/list_child";
    }

    @RequestMapping("/treeAll")
    public
    @ResponseBody
    List<ChannelTree> tree() {
        return channelService.generateTree();
    }

    /**
     * 异步
     *
     * @param pid
     * @return
     */
    @RequestMapping(value = "/treeAs", method = RequestMethod.POST)
    public
    @ResponseBody
    List<TreeDto> tree(@Param Integer pid) {
        List<TreeDto> treeDtos = new ArrayList<TreeDto>();

        if (pid == null) {
            treeDtos.add(new TreeDto(0, "网站根栏目", 1));
            return treeDtos;
        }
        List<ChannelTree> channelTrees = channelService.generateTreeByParent(pid);
        for (ChannelTree channelTree : channelTrees) {
            treeDtos.add(new TreeDto(channelTree.getId(), channelTree.getName(), 1));
        }
        return treeDtos;
    }

    private void initAdd(Model model, Integer pid) {
        if (pid == null) {
            pid = 0;
        }
        Channel pc = null;
        if (pid == 0) {
            pc = new Channel();
            pc.setId(Channel.ROOT_ID);
            pc.setName(Channel.ROOT_NAME);

        } else {
            pc = channelService.load(pid);
        }
        model.addAttribute("pc", pc);
        model.addAttribute("types", EnumUtil.enumProp2NameMap(ChannelType.class, "name"));
    }

    @RequestMapping(value = "/add/{pid}", method = RequestMethod.GET)
    public String add(@PathVariable Integer pid, Model model) {
        initAdd(model, pid);
        model.addAttribute(new Channel());
        return "channel/add";
    }

    @RequestMapping(value = "/add/{pid}", method = RequestMethod.POST)
    public String add(@PathVariable Integer pid, Channel channel, BindingResult bindingResult, Model model) {
        if (bindingResult.hasErrors()) {
            initAdd(model, pid);
            return "channel/add";
        }
        channelService.add(channel, pid);
        return "redirect:/admin/channel/channels/" + pid + "?refresh=1";
    }

    @RequestMapping(value = "/delete/{pid}/{id}")
    public String delete(@PathVariable Integer pid, @PathVariable Integer id) {
        channelService.delete(id);
        return "redirect:/admin/channel/channels/" + pid + "?refresh=1";
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable Integer id, Model model) {
        Channel c = channelService.load(id);
        model.addAttribute("channel", c);
        Channel pc = null;
        if (c.getParent() == null) {
            pc = new Channel();
            pc.setId(Channel.ROOT_ID);
            pc.setName(Channel.ROOT_NAME);
        } else {
            pc = c.getParent();
        }
        model.addAttribute("pc", pc);
        model.addAttribute("types", EnumUtil.enumProp2NameMap(ChannelType.class, "name"));
        return "channel/update";
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    public String update(@PathVariable Integer id, Channel channel, BindingResult bindingResult, Model model) {
        if (bindingResult.hasErrors()) {
            model.addAttribute("types", EnumUtil.enumProp2NameMap(ChannelType.class, "name"));
            return "channel/update";
        }
        Channel tc = channelService.load(id);
        int pid = 0;
        if (tc.getParent() != null) {
            pid = tc.getParent().getId();
        }
        tc.setCustomLink(channel.getCustomLink());
        tc.setCustomLinkUrl(channel.getCustomLinkUrl());
        tc.setIsIndex(channel.getIsIndex());
        tc.setIsTopNav(channel.getIsTopNav());
        tc.setName(channel.getName());
        tc.setRecommend(channel.getRecommend());
        tc.setStatus(channel.getStatus());
        tc.setType(channel.getType());
        channelService.update(tc);
        return "redirect:/admin/channel/channels/" + pid + "?refresh=1";
    }

    @RequestMapping("/channels/updateSort")
    public
    @ResponseBody
    AjaxObj updateSort(@Param Integer[] ids) {
        try {
            channelService.updateSort(ids);
        } catch (Exception e) {
            return new AjaxObj(0, e.getMessage());
        }
        return new AjaxObj(1);
    }
}
